<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="项目名称" prop="pname">
        <el-input
          v-model="queryParams.pname"
          placeholder="请输入项目名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="assistant">
        <el-input
          v-model="queryParams.assistant"
          placeholder="请输入项目负责人名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:project:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:project:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" stripe @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" fixed width="55" align="center" show-overflow-tooltip />
      <el-table-column label="序号" fixed type="index" width="55" align="center" show-overflow-tooltip />
      <el-table-column label="部门名称" fixed align="center" prop="deptName" show-overflow-tooltip />
      <el-table-column label="部门负责人" fixed align="center" prop="leader" show-overflow-tooltip />
      <el-table-column label="项目负责人" align="center" prop="assistant" show-overflow-tooltip />
      <el-table-column label="项目名称" align="center" prop="pname" show-overflow-tooltip>
        <template slot-scope="scope">
          <span @dblclick="copyText(scope.row.pname)">{{ scope.row.pname }}</span>
        </template>
      </el-table-column>
      <!-- clipboard -->
      <el-table-column label="委托单位" align="center" prop="clientName" show-overflow-tooltip />
      <el-table-column label="估算金额" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.planprice }} 元
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" show-overflow-tooltip prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 || scope.row.status === 1 ? '' : (scope.row.status === 3 ? 'success' : 'danger')">{{ selectDictLabel(dict.type.sys_approval_status, scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="opinion" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" fixed="right" width="240" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:project:edit']"
            v-if="scope.row.status == 0 || scope.row.status == 2"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="getProjectDetail(scope.row.id)"
            v-hasPermi="['system:project:detail']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="handleSend(scope.row)"
            v-hasPermi="['system:project:send']"
            v-if="scope.row.status == 0 || scope.row.status == 2"
          >发送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:project:remove']"
            v-if="scope.row.status == 0 || scope.row.status == 2"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改洽谈申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="form.deptName" disabled placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="项目名称" prop="pname">
              <el-input v-model="form.pname" :disabled="disableInput" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="所属板块" prop="plateId">
              <el-select v-model="form.plateId" placeholder="请选择所属板块" :disabled="disableInput" @change="getClientList">
                <el-option
                  v-for="item in plateList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="委托单位" prop="clientId">
              <el-select v-model="form.clientId" :disabled="disableInput" placeholder="请选择委托单位">
                <el-option
                  v-for="item in clientList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="资金来源" prop="sourcefunds">
              <el-select v-model="form.sourcefunds" :disabled="disableInput" placeholder="请选择资金来源">
                <el-option
                  v-for="dict in dict.type.sys_nego_source"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="部门负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入部门负责人" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="项目负责人" prop="assistant">
              <el-input v-model="form.assistant" :disabled="disableInput" placeholder="请输入项目负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="甲方负责人" prop="jf">
              <el-input v-model="form.jf" :disabled="disableInput" placeholder="请输入甲方负责人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="甲方联系电话" title="甲方联系电话" prop="jfPhone">
              <el-input v-model="form.jfPhone" :disabled="disableInput" placeholder="请输入甲方联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="委托时间" prop="clientTime">
              <el-date-picker 
               :disabled="disableInput"
                v-model="form.clientTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择委托时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="招标范围或主要技术参数" title="招标范围或主要技术参数" prop="tcpa">
              <el-input v-model="form.tcpa" type="textarea" rows="2" :disabled="disableInput" placeholder="请输入招标范围或主要技术参数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="组织方式" prop="bidmethod">
              <el-select v-model="form.bidmethod" :disabled="disableInput" placeholder="请输入组织方式">
                <el-option
                  v-for="dict in dict.type.sys_bidmethos"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="招标类别" prop="bidcategory">
              <el-select v-model="form.bidcategory" :disabled="disableInput" placeholder="请输入招标类别">
                <el-option
                  v-for="dict in dict.type.sys_bidcategory"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="工程类别">
              <el-select v-model="form.enginer" :disabled="disableInput" clearable placeholder="请输入工程类别">
                <el-option
                  v-for="dict in dict.type.sys_enginer"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="估算金额" prop="planprice">
              <el-input class="pirce" v-model="form.planprice" :disabled="disableInput" placeholder="请输入估算金额"/> 元
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="是否外揽" prop="decide">
              <el-radio-group v-model="form.decide" :disabled="disableInput" placeholder="请输入是否外揽">
                <el-radio :label="dict.value" v-for="dict in dict.type.is_switch" :key="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="是否依法" prop="law">
              <el-radio-group v-model="form.law" :disabled="disableInput" placeholder="请输入是否依法">
                <el-radio :label="Number(dict.value)" v-for="dict in dict.type.is_switch" :key="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" :disabled="disableInput" type="textarea" rows="2" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23" v-if="disableInput && form.opinion">
            <el-form-item label="审批意见" prop="remark">
              <el-input v-model="form.opinion" disabled type="textarea" rows="2" placeholder="请输入审批意见" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject, sendProject } from "@/api/projectManage/negoApplication";
import { listPlate } from "@/api/infoMaintain/plateManage";
import { listClient } from "@/api/infoMaintain/entrustUnit";
import { mapGetters } from "vuex";
export default {
  name: "Project",
  dicts: ['is_switch', 'sys_approval_status', 'sys_nego_source', 'sys_bidmethos', 'sys_bidcategory', 'sys_enginer'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 洽谈申请表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pname: null,
        plateId: null,
        clientId: null,
        assistant: null,
        deptId: null,
        status: '1'
      },
      // 是否禁用表单输入的各项内容
      disableInput: false,
      // 板块列表
      plateList: [],
      // 委托单位列表
      clientList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        pname: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        plateId: [
          { required: true, message: "所属板块不能为空", trigger: "change" }
        ],
        clientId: [
          { required: true, message: "委托单位不能为空", trigger: "change" }
        ],
        sourcefunds: [
          { required: true, message: "资金来源不能为空", trigger: "change" }
        ],
        leader: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" }
        ],
        assistant: [
          { required: true, message: "项目负责人不能为空", trigger: "blur" }
        ],
        jf: [
          { required: true, message: "甲方负责人不能为空", trigger: "blur" }
        ],
        jfPhone: [
          { required: true, message: "甲方联系电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        clientTime: [
          { required: true, message: "委托时间不能为空", trigger: "change" }
        ],
        bidmethod: [
          { required: true, message: "组织方式不能为空", trigger: "change" }
        ],
        tcpa: [
          { required: true, message: "招标范围或主要技术参数不能为空", trigger: "blur" }
        ],
        bidcategory: [
          { required: true, message: "招标类别不能为空", trigger: "change" }
        ],
        planprice: [
          { required: true, message: "估算金额不能为空", trigger: "blur" }
        ],
        decide: [
          { required: true, message: "是否外揽不能为空", trigger: "change" }
        ],
        law: [
          { required: true, message: "是否依法不能为空", trigger: "change" }
        ],
      }
    };
  },
  computed: {
    ...mapGetters(['roles', 'userInfo'])
  },
  created() {
    this.getPlateList()
    if (this.roles && (this.roles.indexOf('zb_b') > -1 || this.roles.indexOf('zb_f') > -1)) {
      this.queryParams.deptId = this.userInfo.deptId
    } else {
      this.queryParams.deptId = null
    }
    this.getList()
  },
  methods: {
    /** 查询洽谈申请列表 */
    getList() {
      this.loading = true
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows
        this.total = response.total
        this.loading = false
      });
    },
    // 获取板块
    getPlateList() {
      listPlate({ pageNum: 1, pageSize: 1000 }).then(response => {
        this.plateList = response.rows
      })
    },
    // 获取委托单位
    getClientList(plateId) {
      listClient({ pageNum: 1, pageSize: 100000, plateId }).then(response => {
        this.clientList = response.rows
      });
    },
    // 筛选板块查询
    plateChangeQuery(id) {
      this.getClientList(id)
      this.handleQuery()
    },
    getProjectDetail(id, flag) {
      getProject(id).then(response => {
        this.form = response.data;
        this.getClientList(response.data.plateId)
        this.open = true;
        if (flag) {
          this.title = '修改洽谈申请'
          this.disableInput = false
        } else {
          this.title = '查看洽谈申请'
          this.disableInput = true
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: this.userInfo.userId,
        plateId: null,
        plateName: null,
        clientId: null,
        clientName: null,
        sourcefunds: null,
        deptId: this.userInfo.deptId,
        deptName: this.userInfo.dept.deptName,
        leader: this.userInfo.dept.leader,
        assistant: null,
        jf: null,
        jfPhone: null,
        pname: null,
        clientTime: null,
        tcpa: null,
        bidmethod: null,
        bidcategory: null,
        enginer: null,
        planprice: null,
        decide: "0",
        law: 0,
        status: '0',
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
      console.log('this.form', this.form)
      
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.disableInput = false
      this.title = "添加洽谈申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getProjectDetail(id, 'exit')
    },
    /** 发送操作 */
    async handleSend(row) {
      const res = await sendProject({ id: row.id, status: 1 })
      if (res.code == 200) {
        this.$modal.msgSuccess("发送成功");
      }
      this.getList()
    },
    /** 提交按钮 */
    submitForm() {
      if (this.disableInput) {
        this.open = false
        return
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除洽谈申请编号为"' + ids + '"的数据项？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/project/export', {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    },
    async copyText(textToCopy) {
      // navigator clipboard 需要https等安全上下文
      if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪贴板写文本
        navigator.clipboard.writeText(textToCopy).then(() => {
          this.$modal.msgSuccess('复制成功！')
        })
      } else {
        // 创建text area
        let textArea = document.createElement("textarea");
        textArea.value = textToCopy;
        // 使text area不在viewport，同时设置不可见
        textArea.style.position = "absolute";
        textArea.style.opacity = '0';
        textArea.style.left = "-999999px";
        textArea.style.top = "-999999px";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        let res = new Promise((res, rej) => {
          res(() => {
            document.execCommand('copy')
            textArea.remove();
          })
          rej(false)
        })
        res.then(res => {
          res();
          this.$modal.msgSuccess('复制成功！')
        }).catch(error => {
          this.$modal.msgError('复制失败！')
        })
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.opinion {
  color: #F56C6C;
}
.el-date-editor, .el-input, .el-select {
  width: 100%;
}
::v-deep .el-dialog {
  .el-dialog__body {
    padding: 30px 10px 30px 30px;
  }
}
::v-deep .el-form {
  .el-form-item {
    .el-form-item__label {
      max-width: 100px; /* 你可以根据需要调整这个值 */
      white-space: nowrap; /* 防止文字换行 */
      overflow: hidden; /* 超出部分隐藏 */
      text-overflow: ellipsis; /* 文字溢出时显示省略号 */
    }
    .is-disabled{
      .el-textarea__inner, .el-input__inner {
        color: #333333;
      }
      .el-radio__label {
        color: #333333;
      }
      .el-radio__inner {
        border-color: #333333;
        &::after {
          background: #333333;
        }
      }
    }
    .pirce {
      width: 94%;
    }
  }
}
</style>
